{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-01-07T14:26:18.335843Z",
     "start_time": "2025-01-07T14:26:18.028152Z"
    }
   },
   "source": [
    "import numpy as np\n",
    "# 创建一个nan和inf #\n",
    "a = np.nan\n",
    "b = np.inf# inf 表示无穷大，它是一个表示极大值或极小值的特殊值\n",
    "print(a, type(a))\n",
    "print(b, type(b))"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "nan <class 'float'>\n",
      "inf <class 'float'>\n"
     ]
    }
   ],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T14:28:28.336644Z",
     "start_time": "2025-01-07T14:28:28.316618Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "\n",
    "# ----------练习： 处理数组中nan\n",
    "t = np.arange(24).reshape(4, 6).astype('float')\n",
    "#\n",
    "# 将数组中的一部分替换nan\n",
    "t[1, 3:] = np.nan # 这里将第 1 行（索引 1）从第 4 列开始的所有元素替换为 nan\n",
    "print(t)\n",
    "print('-------------------')\n",
    "print(t.shape)\n",
    "print(id(t))"
   ],
   "id": "8cf825cea3eaaabc",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8. nan nan nan]\n",
      " [12. 13. 14. 15. 16. 17.]\n",
      " [18. 19. 20. 21. 22. 23.]]\n",
      "-------------------\n",
      "(4, 6)\n",
      "1815734269872\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T14:30:20.342135Z",
     "start_time": "2025-01-07T14:30:20.327437Z"
    }
   },
   "cell_type": "code",
   "source": [
    "for i in range(t.shape[1]):\n",
    "     # 获取当前列的数据，t[:, i] 选取第 i 列\n",
    "    temp_col = t[:, i]\n",
    "\n",
    "    # 判断当前列的数据中是否含有 nan\n",
    "    # np.count_nonzero(temp_col != temp_col) 用于计算当前列中 nan 的数量\n",
    "    nan_num = np.count_nonzero(temp_col != temp_col)\n",
    "   # 如果 nan_num 不等于 0，说明这一列含有 nan\n",
    "    if nan_num != 0:\n",
    "        # 将这一列不为nan的数据拿出来,并计算平均值\n",
    "        temp_col_not_nan = temp_col[temp_col == temp_col]\n",
    "        print(temp_col_not_nan)\n",
    "        # 将nan替换成这一列的平均值\n",
    "        temp_col[np.isnan(temp_col)] = np.mean(temp_col_not_nan)\n",
    "print('-'*50)\n",
    "print(t)\n",
    "print(id(t))"
   ],
   "id": "74c39382dd9887b1",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 3. 15. 21.]\n",
      "[ 4. 16. 22.]\n",
      "[ 5. 17. 23.]\n",
      "--------------------------------------------------\n",
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8. 13. 14. 15.]\n",
      " [12. 13. 14. 15. 16. 17.]\n",
      " [18. 19. 20. 21. 22. 23.]]\n",
      "1815734269872\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T14:32:57.010948Z",
     "start_time": "2025-01-07T14:32:56.998838Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "\n",
    "# 创建一个包含 12 个元素的数组，并将其重塑为 3 行 4 列的二维数组\n",
    "a = np.arange(12).reshape(3, 4)\n",
    "\n",
    "# 打印原始数组\n",
    "print('原数组：')\n",
    "print(a)\n",
    "print('\\n')  # 打印一个换行符，用于分隔输出\n",
    "\n",
    "# 使用 np.transpose() 对数组进行转置\n",
    "print('对换数组：')\n",
    "print(np.transpose(a))  # np.transpose() 将行列互换\n",
    "print(a)  # 打印原数组，验证原数组未被修改\n",
    "\n",
    "# 重置数组，重新生成一个 3 行 4 列的二维数组\n",
    "a = np.arange(12).reshape(3, 4)\n",
    "\n",
    "# 打印原始数组\n",
    "print('原数组：')\n",
    "print(a)\n",
    "print('\\n')  # 打印一个换行符，用于分隔输出\n",
    "\n",
    "# 使用 .T 属性对数组进行转置\n",
    "print('转置数组：')\n",
    "print(a.T)  # .T 是数组的转置属性，效果与 np.transpose() 相同"
   ],
   "id": "e1d2d267a7d2c039",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原数组：\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "\n",
      "\n",
      "对换数组：\n",
      "[[ 0  4  8]\n",
      " [ 1  5  9]\n",
      " [ 2  6 10]\n",
      " [ 3  7 11]]\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "原数组：\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "\n",
      "\n",
      "转置数组：\n",
      "[[ 0  4  8]\n",
      " [ 1  5  9]\n",
      " [ 2  6 10]\n",
      " [ 3  7 11]]\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T14:34:36.062831Z",
     "start_time": "2025-01-07T14:34:36.054629Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "\n",
    "# 创建一个四维数组，形状为 (3, 4, 5, 6)，所有元素的值为 1\n",
    "a = np.ones((3, 4, 5, 6))\n",
    "\n",
    "# 使用 np.rollaxis 将轴移动\n",
    "# 这里将原数组的第 4 维 (轴索引为 3) 移动到第 2 维的位置 (索引为 1)\n",
    "b = np.rollaxis(a, 3, 1)\n",
    "\n",
    "# 打印移动后数组的形状\n",
    "print(b.shape)\n"
   ],
   "id": "7889ff595584835d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3, 6, 4, 5)\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T14:35:27.085442Z",
     "start_time": "2025-01-07T14:35:25.546671Z"
    }
   },
   "cell_type": "code",
   "source": "import pandas as pd",
   "id": "6aecb87d6f4569ab",
   "outputs": [],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T14:36:56.928965Z",
     "start_time": "2025-01-07T14:36:56.917734Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "\n",
    "# 创建一个 Pandas Series 对象，值是 10 到 19 的范围，默认索引为 0 到 9\n",
    "ser_obj = pd.Series(range(10, 20))\n",
    "\n",
    "# 打印 Series 对象\n",
    "print(ser_obj)  # 打印输出会附带数据类型信息\n"
   ],
   "id": "2aec09a39c534ec8",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    10\n",
      "1    11\n",
      "2    12\n",
      "3    13\n",
      "4    14\n",
      "5    15\n",
      "6    16\n",
      "7    17\n",
      "8    18\n",
      "9    19\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T14:38:40.048850Z",
     "start_time": "2025-01-07T14:38:40.030323Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 获取数据部分（Series 的值）\n",
    "print(ser_obj.values)  # Series 的值实际存储为 NumPy 的 ndarray\n",
    "print(type(ser_obj.values))  # 验证数据的类型是 ndarray\n",
    "\n",
    "# 获取索引部分（Series 的索引）\n",
    "print(ser_obj.index)  # Series 的索引是内部的 RangeIndex 类型\n",
    "\n",
    "# 查看数据类型（dtype）\n",
    "ser_obj.dtype  # 返回 Series 的数据类型\n"
   ],
   "id": "3212dd82d0185074",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10 11 12 13 14 15 16 17 18 19]\n",
      "<class 'numpy.ndarray'>\n",
      "RangeIndex(start=0, stop=10, step=1)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "dtype('int64')"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T14:39:18.217518Z",
     "start_time": "2025-01-07T14:39:18.203622Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(ser_obj * 2)  #元素级乘法\n",
    "print(ser_obj > 15) #返回一个bool序列"
   ],
   "id": "ebb119b9dc5ef5c1",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    20\n",
      "1    22\n",
      "2    24\n",
      "3    26\n",
      "4    28\n",
      "5    30\n",
      "6    32\n",
      "7    34\n",
      "8    36\n",
      "9    38\n",
      "dtype: int64\n",
      "0    False\n",
      "1    False\n",
      "2    False\n",
      "3    False\n",
      "4    False\n",
      "5    False\n",
      "6     True\n",
      "7     True\n",
      "8     True\n",
      "9     True\n",
      "dtype: bool\n"
     ]
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T14:40:13.845820Z",
     "start_time": "2025-01-07T14:40:13.825197Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "\n",
    "# 创建一个字典，其中键是年份，值是对应的数据\n",
    "year_data = {2001: 17.8, 2005: 20.1, 2003: 16.5}\n",
    "\n",
    "# 使用字典创建一个 Pandas Series\n",
    "ser_obj2 = pd.Series(year_data)\n",
    "\n",
    "# 打印 Series，查看其内容\n",
    "print(ser_obj2)\n",
    "\n",
    "# 打印分隔线，用于分隔不同的输出\n",
    "print('-' * 50)\n",
    "\n",
    "# 打印 Series 的索引\n",
    "print(ser_obj2.index)  # 索引为字典的键，这里是年份，类型为 Int64Index\n",
    "\n",
    "# 打印分隔线\n",
    "print('-' * 50)\n",
    "\n",
    "# 获取指定索引（年份 2001）对应的值\n",
    "print(ser_obj2[2001])  # 输出 17.8\n",
    "\n",
    "# 获取 Series 的所有值部分\n",
    "ser_obj2.values  # 返回一个包含所有值的 NumPy 数组\n"
   ],
   "id": "f87fe57477a0a3f4",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2001    17.8\n",
      "2005    20.1\n",
      "2003    16.5\n",
      "dtype: float64\n",
      "--------------------------------------------------\n",
      "Index([2001, 2005, 2003], dtype='int64')\n",
      "--------------------------------------------------\n",
      "17.8\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([17.8, 20.1, 16.5])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T14:40:37.079593Z",
     "start_time": "2025-01-07T14:40:37.064420Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "\n",
    "# 通过ndarray构建DataFrame\n",
    "t = pd.DataFrame(np.arange(12).reshape((3,4))) #默认索引是0-2\n",
    "print(t)"
   ],
   "id": "d4e8ed54c1364512",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   0  1   2   3\n",
      "0  0  1   2   3\n",
      "1  4  5   6   7\n",
      "2  8  9  10  11\n"
     ]
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T14:41:16.160255Z",
     "start_time": "2025-01-07T14:41:16.146155Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "# 使用 NumPy 的 random.randn() 方法生成一个形状为 (5, 4) 的数组\n",
    "# 该数组的值是从标准正态分布（均值为0，标准差为1）中随机生成的\n",
    "array = np.random.randn(5, 4)\n",
    "\n",
    "# 打印生成的 NumPy 数组\n",
    "print(array)\n",
    "\n",
    "# 打印分隔线，便于区分不同输出\n",
    "print('-' * 50)\n",
    "\n",
    "# 使用 NumPy 数组创建一个 Pandas DataFrame 对象\n",
    "df_obj = pd.DataFrame(array)\n",
    "\n",
    "# 打印 DataFrame 的前 5 行\n",
    "# 默认情况下，head() 显示 DataFrame 的前 5 行（如果数据行数少于 5 行，则显示所有行）\n",
    "print(df_obj.head())  # 打印 DataFrame 的前 5 行\n"
   ],
   "id": "bd70b4e1aefa4125",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-1.73209374 -1.58115504 -0.3621713   0.29587743]\n",
      " [-0.32235219  0.60968655 -0.94061876  0.91019056]\n",
      " [-1.53050557  0.4009002  -0.14477011 -1.2194257 ]\n",
      " [ 0.47193888  0.92538922 -0.5810015  -1.21541448]\n",
      " [-0.40155191 -1.42360771  1.39638911  0.23276248]]\n",
      "--------------------------------------------------\n",
      "          0         1         2         3\n",
      "0 -1.732094 -1.581155 -0.362171  0.295877\n",
      "1 -0.322352  0.609687 -0.940619  0.910191\n",
      "2 -1.530506  0.400900 -0.144770 -1.219426\n",
      "3  0.471939  0.925389 -0.581002 -1.215414\n",
      "4 -0.401552 -1.423608  1.396389  0.232762\n"
     ]
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T14:41:37.603400Z",
     "start_time": "2025-01-07T14:41:37.593611Z"
    }
   },
   "cell_type": "code",
   "source": "t.loc[0] #单独把某一行取出来,类型是series",
   "id": "d6282ce8597bc6af",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    0\n",
       "1    1\n",
       "2    2\n",
       "3    3\n",
       "Name: 0, dtype: int64"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T14:42:24.224614Z",
     "start_time": "2025-01-07T14:42:24.211590Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "\n",
    "# 创建一个包含字典的列表，每个字典表示一个人的信息\n",
    "# 有些字典缺失了一些键（例如年龄或电话号码）\n",
    "d2 = [\n",
    "    {\"name\": \"xiaohong\", \"age\": 32, \"tel\": 10010},\n",
    "    {\"name\": \"xiaogang\", \"tel\": 10000},\n",
    "    {\"name\": \"xiaowang\", \"age\": 22}\n",
    "]\n",
    "\n",
    "# 使用字典列表创建一个 Pandas DataFrame\n",
    "df6 = pd.DataFrame(d2)\n",
    "\n",
    "# 打印 DataFrame，查看数据结构\n",
    "print(df6)  # 缺失的值会自动用 NaN 填充\n",
    "\n",
    "# 打印 DataFrame 值部分的类型\n",
    "print(type(df6.values))  # values 返回 NumPy ndarray 类型\n"
   ],
   "id": "af25936f0e72c88a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       name   age      tel\n",
      "0  xiaohong  32.0  10010.0\n",
      "1  xiaogang   NaN  10000.0\n",
      "2  xiaowang  22.0      NaN\n",
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "execution_count": 14
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T14:43:06.739312Z",
     "start_time": "2025-01-07T14:43:06.720686Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "# 创建一个字典，其中包含各种数据类型\n",
    "dict_data = {\n",
    "    'A': 1,  # 整数类型的标量\n",
    "    'B': pd.Timestamp('20190926'),  # 一个单独的时间戳\n",
    "    'C': pd.Series(1, index=list(range(4)), dtype='float32'),  # 一个长度为 4 的 Series，类型为 float32\n",
    "    'D': np.array([1, 2, 3, 4], dtype='int32'),  # 一个 NumPy 数组，类型为 int32\n",
    "    'E': [\"Python\", \"Java\", \"C++\", \"C\"],  # 一个包含字符串的列表\n",
    "    'F': 'wangdao'  # 一个字符串标量\n",
    "}\n",
    "\n",
    "# 使用字典数据创建一个 Pandas DataFrame\n",
    "df_obj2 = pd.DataFrame(dict_data)\n",
    "\n",
    "# 打印输出 DataFrame\n",
    "print(df_obj2)\n"
   ],
   "id": "b03df2892d7278c9",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   A          B    C  D       E        F\n",
      "0  1 2019-09-26  1.0  1  Python  wangdao\n",
      "1  1 2019-09-26  1.0  2    Java  wangdao\n",
      "2  1 2019-09-26  1.0  3     C++  wangdao\n",
      "3  1 2019-09-26  1.0  4       C  wangdao\n"
     ]
    }
   ],
   "execution_count": 15
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T14:45:32.242486Z",
     "start_time": "2025-01-07T14:45:32.229722Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "# 创建一个 DataFrame，包含多种类型的数据\n",
    "# 每一行代表一个列表，列表中包含了 3 个元素\n",
    "df_data = pd.DataFrame([\n",
    "    np.random.randn(3),  # 第一行：随机生成 3 个数，来自标准正态分布\n",
    "    [1., 2., np.nan],    # 第二行：包含数值和一个缺失值 (NaN)\n",
    "    [np.nan, 4., np.nan],  # 第三行：包含 NaN 和一个数值\n",
    "    [1., 2., 3.]         # 第四行：包含 3 个数值\n",
    "])\n",
    "\n",
    "# 打印 DataFrame 的前 5 行（在此情况下为前 4 行）\n",
    "print(df_data.head())\n"
   ],
   "id": "117bc3f465be097e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          0         1         2\n",
      "0  0.353658 -0.999485 -0.564006\n",
      "1  1.000000  2.000000       NaN\n",
      "2       NaN  4.000000       NaN\n",
      "3  1.000000  2.000000  3.000000\n"
     ]
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T14:46:20.938571Z",
     "start_time": "2025-01-07T14:46:20.929779Z"
    }
   },
   "cell_type": "code",
   "source": "df_data.iloc[2,0]",
   "id": "f37e7b9c90ff316c",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(nan)"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 17
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T14:46:31.630890Z",
     "start_time": "2025-01-07T14:46:31.621996Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#isnull来判断是否有空的数据\n",
    "print(df_data.isnull())"
   ],
   "id": "131e03ea649e9bc0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       0      1      2\n",
      "0  False  False  False\n",
      "1  False  False   True\n",
      "2   True  False   True\n",
      "3  False  False  False\n"
     ]
    }
   ],
   "execution_count": 18
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T14:46:43.201117Z",
     "start_time": "2025-01-07T14:46:43.193054Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#帮我计算df_data缺失率\n",
    "print(df_data.isnull().sum()/len(df_data))"
   ],
   "id": "c3ef0cb76586d9e5",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    0.25\n",
      "1    0.00\n",
      "2    0.50\n",
      "dtype: float64\n"
     ]
    }
   ],
   "execution_count": 19
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T14:47:51.723929Z",
     "start_time": "2025-01-07T14:47:51.687159Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "# 使用 MultiIndex 创建一个复合索引，包含两个层次：'cloth' 和 'size'\n",
    "# 第一个层次 'cloth' 有 'a', 'b', 'c', 'd' 四个不同的值\n",
    "# 第二个层次 'size' 有 0, 1, 2，表示每种 'cloth' 对应 3 个不同的大小\n",
    "index1 = pd.MultiIndex.from_arrays([\n",
    "    ['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c', 'd', 'd', 'd'],  # cloth（布料类型）\n",
    "    [0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2]  # size（大小）\n",
    "], names=['cloth', 'size'])\n",
    "\n",
    "# 创建一个包含随机数的 Pandas Series，并将 MultiIndex 作为索引\n",
    "ser_obj = pd.Series(np.random.randn(12), index=index1)\n",
    "\n",
    "# 使用 unstack() 方法将 Series 转换为 DataFrame，0 层索引 ('cloth') 会成为列名\n",
    "df_obj = ser_obj.unstack(0)\n",
    "\n",
    "# 打印 DataFrame\n",
    "print(df_obj)\n",
    "\n",
    "# 修改 DataFrame 中某个元素，将 (0, 'b') 位置的值设置为 NaN\n",
    "df_obj.loc[0, 'b'] = np.nan\n",
    "\n",
    "# 打印修改后的 DataFrame\n",
    "print(df_obj)\n",
    "\n",
    "# 打印分隔线，便于区分不同输出\n",
    "print('-' * 50)\n",
    "\n",
    "# 计算 DataFrame 每列的最小值（忽略缺失值 NaN），axis=0 表示按列计算\n",
    "print(df_obj.min(axis=0))\n"
   ],
   "id": "6343d1e0ce626fff",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "cloth         a         b         c         d\n",
      "size                                         \n",
      "0      1.923939 -0.841462  1.463757 -0.099167\n",
      "1     -1.361426 -0.266408 -0.183912  0.544377\n",
      "2      0.042796 -0.367312 -0.413000  0.479093\n",
      "cloth         a         b         c         d\n",
      "size                                         \n",
      "0      1.923939       NaN  1.463757 -0.099167\n",
      "1     -1.361426 -0.266408 -0.183912  0.544377\n",
      "2      0.042796 -0.367312 -0.413000  0.479093\n",
      "--------------------------------------------------\n",
      "cloth\n",
      "a   -1.361426\n",
      "b   -0.367312\n",
      "c   -0.413000\n",
      "d   -0.099167\n",
      "dtype: float64\n"
     ]
    }
   ],
   "execution_count": 20
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T14:48:09.965252Z",
     "start_time": "2025-01-07T14:48:09.951851Z"
    }
   },
   "cell_type": "code",
   "source": "df_obj",
   "id": "c7f0a5791daa1c5d",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "cloth         a         b         c         d\n",
       "size                                         \n",
       "0      1.923939       NaN  1.463757 -0.099167\n",
       "1     -1.361426 -0.266408 -0.183912  0.544377\n",
       "2      0.042796 -0.367312 -0.413000  0.479093"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>cloth</th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "      <th>c</th>\n",
       "      <th>d</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>size</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.923939</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.463757</td>\n",
       "      <td>-0.099167</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-1.361426</td>\n",
       "      <td>-0.266408</td>\n",
       "      <td>-0.183912</td>\n",
       "      <td>0.544377</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.042796</td>\n",
       "      <td>-0.367312</td>\n",
       "      <td>-0.413000</td>\n",
       "      <td>0.479093</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 21
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T14:48:24.229204Z",
     "start_time": "2025-01-07T14:48:24.207872Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#观察数据分布的一种 非常常用方法\n",
    "print(df_obj.describe())"
   ],
   "id": "e1071508a04d6b0e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "cloth         a         b         c         d\n",
      "count  3.000000  2.000000  3.000000  3.000000\n",
      "mean   0.201770 -0.316860  0.288949  0.308101\n",
      "std    1.648442  0.071350  1.023842  0.354212\n",
      "min   -1.361426 -0.367312 -0.413000 -0.099167\n",
      "25%   -0.659315 -0.342086 -0.298456  0.189963\n",
      "50%    0.042796 -0.316860 -0.183912  0.479093\n",
      "75%    0.983368 -0.291634  0.639923  0.511735\n",
      "max    1.923939 -0.266408  1.463757  0.544377\n"
     ]
    }
   ],
   "execution_count": 22
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T14:48:32.388644Z",
     "start_time": "2025-01-07T14:48:32.377088Z"
    }
   },
   "cell_type": "code",
   "source": "df_obj.loc[:,'c']",
   "id": "63974cda88c21e6c",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "size\n",
       "0    1.463757\n",
       "1   -0.183912\n",
       "2   -0.413000\n",
       "Name: c, dtype: float64"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 23
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T14:48:42.867910Z",
     "start_time": "2025-01-07T14:48:42.860567Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#计算最小值或者最大值的索引的位置\n",
    "print(df_obj.loc[:,'c'].argmin())#argmin和argmax只能是series，不能是df"
   ],
   "id": "e4d8e72ba91cec94",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "execution_count": 24
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T14:48:58.583784Z",
     "start_time": "2025-01-07T14:48:58.568511Z"
    }
   },
   "cell_type": "code",
   "source": "df_obj",
   "id": "253cc6aa7e25d544",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "cloth         a         b         c         d\n",
       "size                                         \n",
       "0      1.923939       NaN  1.463757 -0.099167\n",
       "1     -1.361426 -0.266408 -0.183912  0.544377\n",
       "2      0.042796 -0.367312 -0.413000  0.479093"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>cloth</th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "      <th>c</th>\n",
       "      <th>d</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>size</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.923939</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.463757</td>\n",
       "      <td>-0.099167</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-1.361426</td>\n",
       "      <td>-0.266408</td>\n",
       "      <td>-0.183912</td>\n",
       "      <td>0.544377</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.042796</td>\n",
       "      <td>-0.367312</td>\n",
       "      <td>-0.413000</td>\n",
       "      <td>0.479093</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 25
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T14:49:11.399317Z",
     "start_time": "2025-01-07T14:49:11.387902Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#返回的是一个series\n",
    "#计算最小值或者最大值的索引值\n",
    "df_obj.idxmin(axis=0, skipna=True)"
   ],
   "id": "47bcb8c3a20bff07",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "cloth\n",
       "a    1\n",
       "b    2\n",
       "c    2\n",
       "d    0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 26
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
